//
//  ScriptsVC.m
//  sweetbox
//
//  Created by apollo on 15/9/18.
//  Copyright (c) 2015年 proj-m. All rights reserved.
//

#import "ScriptsVC.h"
#import "DBHelper.h"
#import "log.h"
#import "NewScriptVC.h"
#import "ScriptVC.h"

@interface ScriptsVC ()
@property (nonatomic, strong) NSMutableArray *scripts;
- (void)onSaveScript;
@end

@implementation ScriptsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.navigationItem.title = self.book;
    
    
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc addObserver:self
           selector:@selector(onSaveScript)
               name:@"save_script"
             object:nil];
    
    self.scripts = [[NSMutableArray alloc] init];
    UITableView *view = (UITableView *) self.view;
    view.dataSource = self;
    view.delegate = self;
    [self refreshScripts];
    
    
}

- (void) refreshScripts {
    [self.scripts removeAllObjects];
    int scriptNum = [[DBHelper sharedDBHelper] countScript:self.book];
    for (int i=0;i<scriptNum;i++) {
        NSArray *script = [[DBHelper sharedDBHelper] scriptAt:self.book index:i];
        [self.scripts addObject:script];
    }
}

- (void)onSaveScript {
    [self refreshScripts];
    UITableView *view = (UITableView *) self.view;
    [view reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)onAdd:(id)sender {
    NewScriptVC *vc  =(NewScriptVC *) [[UIStoryboard storyboardWithName:@"prescription" bundle:nil] instantiateViewControllerWithIdentifier:@"new_script"];
    vc.book = self.book;
    if (nil != vc) {
        vc.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
        [self presentViewController:vc animated:YES completion:^(){
            LOG_I("After Come into NewBook Page");
        }];
    }
}


#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.scripts.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    NSArray *script = self.scripts[indexPath.row];
    cell.textLabel.text = script[1];
    cell.detailTextLabel.text = script[2];
    cell.selectionStyle = UITableViewCellSelectionStyleNone; // set cell not height by can by touch on tableview
    cell.accessoryType =  UITableViewCellAccessoryDisclosureIndicator; // jump tringle icon
    return cell;
}


#pragma mark - UITableDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"select at %d row",(int)indexPath.row);
    NSArray *script = self.scripts[indexPath.row];
    ScriptVC *vc  =(ScriptVC *) [[UIStoryboard storyboardWithName:@"prescription" bundle:nil] instantiateViewControllerWithIdentifier:@"script"];
    vc.book = self.book;
    vc.scriptTitle = script[1];
    UINavigationController *navVC  =  (UINavigationController *)self.parentViewController;
    [navVC pushViewController:vc animated:YES];
    
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSArray *script = self.scripts[indexPath.row];
        if (nil == script) {
            LOG_E("Cur row not in self.scripts");
            return ;
        }
        [[DBHelper sharedDBHelper] deleteScript:self.book title:script[0]];
        [self.scripts removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];
    }
}



@end
